package iscript.gameFramework.IsoLibarys.data 
{
	import flash.display.DisplayObjectContainer;
	import iscript.coreFramework.data.datatable.DataTable;
	import iscript.coreFramework.debug.ArthropodDebug;
	import iscript.coreFramework.math.Point2D;
	import iscript.coreFramework.util.VectorHelper;
	import iscript.gameFramework.IsoLibarys.display.IsoSprite;
	/**
	 * ...
	 * @author raymond 
	 */
	public class IsoBuilderList 
	{
		private var _container:DisplayObjectContainer;
		private var _isoSpriteList:Vector.<IsoSprite>;
		private var _standardIsoSprite:IsoSprite;
		private var _autoSort:Boolean;
		private var _isoData:DataTable;
		
		public function IsoBuilderList(container:DisplayObjectContainer,isoData:DataTable,autoSort:Boolean=true) 
		{
			//_isoSpriteList = new Array(); 
			_isoSpriteList = new Vector.<IsoSprite>();
			_isoData = isoData;
			_container = container;
			_autoSort = autoSort;
		}
		
		
		public function set autoSort(value:Boolean):void { _autoSort=value; }
		public function get autoSort():Boolean { return _autoSort; }
		
		public function getAllIsoSprite():Vector.<IsoSprite> { return _isoSpriteList; }
		
		public function addItem(newIsoSprite:IsoSprite):void {
			
			if (!_autoSort) {
				 _isoSpriteList.push(newIsoSprite);
				 _container.addChild(newIsoSprite);
				}else{
			
				 if (!_container.contains(newIsoSprite)) {
					 _container.addChild(newIsoSprite);
					 }
				 if (_isoSpriteList.length == 0) {
					 _isoSpriteList.push(newIsoSprite);
					 }else {
					 _matchCurrentIndex = getMiddleIndex();
					 _setDepthLogic(_matchCurrentIndex, newIsoSprite)
					}		  
			}
		}
		
		public function moveItem(newIsoSprite:IsoSprite):void {
			   _container.addChildAt(newIsoSprite, _container.numChildren-1);	
			}
		
		
		public function removeItem(isoSprite:IsoSprite):void {
			var index:int =  _isoSpriteList.indexOf(isoSprite);
			var _isoSceneGrid:IsoBaseData;			
            var isobasedata:IsoBaseData; 
			  
			if (index != -1) {
				var data:IsoBound = isoSprite.getIsoBound();
				 for (var i:int = data.topGrid.x; i <=data.bottomGrid.x; i++) 
						  {
							  for (var j:int = data.topGrid.y; j <=data.bottomGrid.y; j++) 
							  {
								    _isoSceneGrid = IsoBaseData(_isoData.getCell(i,j.toString()));
									 isobasedata =  new IsoBaseData();
									 isobasedata.Bound = _isoSceneGrid.Bound;
									 isobasedata.IsBuilder = false;  //不存在建筑
									 isobasedata.IsWalkable = _isoSceneGrid.IsWalkable;
									 _isoData.updateCell(i, j.toString(), isobasedata);
								 
							  }
						  }
				_isoSpriteList.splice(index, 1);		
			  }
			}	
		  
		public function getMiddleIndex():int {
			  if (_isoSpriteList.length == 0) return 0;
			  return _isoSpriteList.length - 1;
			  // return Math.floor(_isoSpriteList.length / 2);
			  // return  _isoSpriteList[Math.floor(_isoSpriteList.length/2)] as IsoSprite;
			}
		 
        private var _matchCurrentIndex:int = 0;
		
		private function _setDepthLogic(matchCurrentIndex:int, newIsoSprite:IsoSprite):void {
			     var currentIsoSprite:IsoSprite =  _isoSpriteList[matchCurrentIndex];				 
				 var _checkWardOff:Boolean = _isWardOff(newIsoSprite, currentIsoSprite); 
				  switch(_checkWardOff) {
					   case false:
							beforeWardOffLoop:
						     while (!_isWardOff(newIsoSprite, currentIsoSprite)) { 
								   _matchCurrentIndex--;	
								    if (_matchCurrentIndex < 0) {
										_matchCurrentIndex =0;
									   break beforeWardOffLoop;
										}
									currentIsoSprite = _isoSpriteList[_matchCurrentIndex] as IsoSprite;
									if (_isWardOff(newIsoSprite, currentIsoSprite)) { 
										_matchCurrentIndex++;
										 break beforeWardOffLoop;
										}
								 }
						   break;
						case true:
							 AfterWardOffLoop:
							 while (_isWardOff(newIsoSprite, currentIsoSprite)) {
								   _matchCurrentIndex++;
								   if (_matchCurrentIndex == _isoSpriteList.length) {
								       break AfterWardOffLoop;
									   }
								   currentIsoSprite = _isoSpriteList[_matchCurrentIndex] as IsoSprite;
								   if (!_isWardOff(newIsoSprite, currentIsoSprite)) { 
									   _matchCurrentIndex--; 
									   break AfterWardOffLoop;
									   }
								   
								 }
							break;
					 }
			         
					 _isoSpriteList.splice(_matchCurrentIndex, 0, newIsoSprite);
					 _container.addChildAt(newIsoSprite, _matchCurrentIndex);
		    }	
			
	     
		/**
		 *  A 是否挡住 B  
		 * @param	a
		 * @param	b
		 * @return
		 */	
		private function _isWardOff(a:IsoSprite, b:IsoSprite):Boolean {
			     if (a.getGroundRectangle().y > b.getGroundRectangle().y) return true;
			     if (a.getGroundRectangle().y == b.getGroundRectangle().y && a.getGroundRectangle().x > b.getGroundRectangle().x) return true;
			   return false;
			}
		     
			 
		/**
		 * 获得topGid 对应值的IsoSprite (只返回一个建筑)
		 * @param	bound
		 * @return
		 */	 
		public function  getIsoSpriteByPoint(point:Point2D):IsoSprite {
			   for each ( var item:IsoSprite  in _isoSpriteList) {
				       if (item.getIsoBound().topGrid.isEqual(point))
					   return item;
				   }
				return null;
			}	
			
			
	}

}